conf = require("../config")
if not conf.nvim_tree.enabled then return {} end

return {
  "nvim-tree/nvim-tree.lua",
  version = "*",
  -- lazy = true,
  dependencies = {
    -- "nvim-tree/nvim-web-devicons",
    'echasnovski/mini.icons'
  },
  cmd = { "NvimTreeOpen", "NvimTreeOpen" },
  keys = {
    {",t", "<cmd>NvimTreeOpen<cr>", "n", desc = "open nvimTree"},
    {",e", "<cmd>NvimTreeFindFile<cr>", "n", desc = "change the cursor in the nvimtree for the current bufname"}
  },
  config = function ()
    -- set termguicolors to enable highlight groups
    vim.opt.termguicolors = true

    local function on_attach(bufnr)
      local api = require "nvim-tree.api"

      local function opts(desc)
        return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
      end

      -- default mappings
      api.config.mappings.default_on_attach(bufnr)

      -- custom mappings
      vim.keymap.set('n', '?',        api.tree.toggle_help,         opts('Help'))
      vim.keymap.set('n', 's',        api.node.open.vertical,       opts('Open: Vertical Split'))
      vim.keymap.set('n', 'i',        api.node.open.horizontal,     opts('Open: Horizontal Split'))
      vim.keymap.set('n', '<space>c', api.tree.change_root_to_node, opts('Change root to node'))
      vim.keymap.set('n', 'o',        api.node.open.tab,     opts('Open in new tab'))
    end

    require("nvim-tree").setup({
      sort = {
        sorter = "case_sensitive",
      },
      view = {
        width = 30,
      },
      renderer = {
        group_empty = true,
        indent_markers = {
          enable = true,
        },
      },
      filters = {
        dotfiles = true,
      },
      on_attach = on_attach,
      sync_root_with_cwd = true,
      respect_buf_cwd = true,
      update_focused_file = {
        enable = false,
        update_root = true
      },
    })
  end,
}
